var goCarInfo;
require(['grid', 'layout','topBar','date'], function () {
    var $grid = $('#grid');
    var lineId,locationDetailInfoDialog;
  //高级查询
    $top = $('#top');
    $('body').layout();
    $top.topBar({
    	left: [{
            type: 'filter-more',
            search: function () {
            	searchLineMainteInfo(lineId);
            },
            item: [
				{
				    text: '时间快捷',
				    name1: 'beginTime',
				    name2: 'endTime',
				    type: 'dateLine'
				}
            ]
        }]
    });

    
    function lineChange(event, treeId, treeNode) {
        if (treeNode.type === 'LINE') {
            getLineCar(treeNode.id);
            $("#lineName").html(treeNode.name);
            lineId = '';
            lineId = treeNode.id
            searchLineMainteInfo(lineId);
        } else {
            $.messager.alert("提示", "请选择线路进行查看。");
        }
    }

    //左侧树
    commonFun.leftTreeNodeClickObj['lineAnaRelatedCar'] = lineChange;

    //线路车辆  
    function getLineCar(lineId) {
        $.ajaxExt({
            url: "/lineAna/getLineCar.htm",
            data: {'lineId': lineId}
        }).done(function (data) {
            $("#lineCar").html(data.carCnt + "辆");
            $("#onLineCar").html(data.onLineCnt + "辆");
        });
    }
    

    //grid初始化
    function gridInit() {
        $grid.grid({
            url: "/lineAna/getRelatedCarPage.htm",
            colModel: [
                {name: "lineId", label: 'lineId', key: true, hidden: true},
                {name: "carId", label: 'carId', hidden: true},
                {name: "licPltNo", label: '车牌号', width: 160,formatter: function (cellvalue,options,rowObject) {
                	return "<b><a href='javascript:void(0);' onclick='goCarInfo(\"" + rowObject.carId + "\")' class='text-link c-blue'>" + cellvalue + "</a></b>";
                }},
                {name: "carInCd", label: '车辆编号', width: 80},
                {name: "carType", label: '车辆类型', width: 100},
                {name: "carSt", label: '车辆状态(当前)', width: 80},
                {name: "perHunEner1", label: '气( m3)', width: 100, align: "right"},
                {name: "perHunEner2", label: '油 (L)', width: 100, align: "right"},
                {name: "perHunEner3", label: '电( KW/H)', width: 100, align: "right"},
                {name: "rngMile", label: '运行里程(KM)', width: 100, align: "right"},
                {name: "rngOprtDura", label: '运行时长', width: 100},
                {name: "loca", label: '定位', width: 60,
                	sortable: false,
                    align: "center",
                    formatter: function (cellvalue, options, rowObject) {
                        var address;
                        if (rowObject.loLgt <= '0' && rowObject.loLtt <= '0') {
                            address = "未知";
                        } else {
                            address = "<b><a href='javascript:void(0);' onclick='queryLocation(\"" + rowObject.licPltNo + "\",\"" + rowObject.drvName + "\",\"" + rowObject.loLgt + "\",\"" + rowObject.loLtt + "\",\"" + rowObject.loDrc + "\"" + ")' style='color: blue;'>"
                            + "查看详细位置</a></b>";
                        }
                        return address;
                    },
                    unformat: function (value) {
                        return value;
                    },
                    width: '240',
                    fixed: false
                }
            ],
            multiselect: false,
			groupHeader:true,
            height: $(window).height() - 325
        });
        $grid.grid('setGroupHeaders', {
    		useColSpanStyle: true,
    		groupHeaders: [{
    			startColumnName: 'perHunEner1',
    			numberOfColumns: 3,
    			titleText: "<div class='t-header'>百公里能耗</div>"
    		}]
    	});
    }

    //-- 显示 详细位置信息
    queryLocation = function (licPltNo, drvName, beginLoLgt, beginLoLtt, beginLoDrc, type, spedBeginTime, spedEndTime) {
        if (drvName == "undefined") {
            drvName = "未知";
        }
        var data = new Array();// 在这里构造车辆数据的数组
        var obj = new Object();//车辆数据
        obj.licPltNo = licPltNo;//车牌号
        obj.driverName = drvName;//驾驶员
        obj.loLgt = beginLoLgt;//经度
        obj.loLtt = beginLoLtt;//纬度
        obj.direction = beginLoDrc;//方向
        obj.type = type;//异常类型 字符串表示
        obj.beginTime = spedBeginTime;//开始时间
        obj.endTime = spedEndTime;//结束时间
        data.push(obj);
        openLocationDetailInfoDialog("详细位置", data); //弹出的标题和数据
    };

    function openLocationDetailInfoDialog(title, data) {
        if (!locationDetailInfoDialog) {
            locationDetailInfoDialog = $('<div>').dialog({
                title: title,
                url: './report/map/mapLocation.htm',
                height: $(window.parent).height() * 0.8,
                width: $(window.parent).width() * 0.85
            });
        }
        locationDetailInfoDialog.dialog('open');
        locationDetailInfoDialog.dialog('exec', 'showMapLocation', data);
    }
    
    
    
    function searchLineMainteInfo(lineId) {
    	var params = {};
    	params.lineId = lineId;
    	var postData = $("#top").parent().getValues();
    	params.beginAttnTime = postData.beginTime;
    	params.endAttnTime = postData.endTime;
        $grid.grid('setGridParam', {
            datatype: "JSON",
            page: 1,
            postData: params
        }).trigger("reloadGrid");
    }
    
    goCarInfo = function(carId){
    	var data = {};
    	data.carId = carId;
    	$.openTab('6bad880fef164ec6adaea7b18420eb6e','showCarInfo',[data]);
    }

    //layout 布局
    $('body').layout();

    gridInit();


    //如果左侧树有选择车辆则直接查询该车辆数据
    business.execFnByLeftTreeSelectNode('LINE', lineChange);
});